package com.yyairmarkyy.arctool;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import com.yyairmarkyy.arctool.inter.ArcOutsideClickLisitener;
import com.yyairmarkyy.arctool.inter.CenterViewClickListener;
import com.yyairmarkyy.arctool.layoutmanager.CircleLayoutManager;
import com.yyairmarkyy.arctool.window.ArcToolWindow;
import com.yyairmarkyy.arctool.window.MyWindowParamsHelper;

public class MainActivity extends AppCompatActivity {

    private Activity mActivity;
    private WindowManager windowManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        setContentView(R.layout.activity_main);
        createWindow();
        findViewById(R.id.tv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!arcToolWindow.isShow())
                    arcToolWindow.show();
            }
        });

    }


    class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        int count = 20;

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(MainActivity.this).inflate(R.layout.my_image, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            int index = (position + 1) % 6;
            int res = 0;
            switch (index) {
                case 0:
                    res = R.mipmap.item1;
                    break;
                case 1:
                    res = R.mipmap.item2;
                    break;
                case 2:
                    res = R.mipmap.item3;
                    break;
                case 3:
                    res = R.mipmap.item4;
                    break;
                case 4:
                    res = R.mipmap.item5;
                    break;
                case 5:
                    res = R.mipmap.item6;
                    break;
            }
            ((MyViewHolder) holder).imageView.setImageResource(res);
        }


        public void updateCount(int count) {
            this.count = count;
            notifyDataSetChanged();
        }

        @Override
        public int getItemCount() {
            return count;
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            ImageView imageView;

            public MyViewHolder(View itemView) {
                super(itemView);
                imageView = (ImageView) itemView.findViewById(R.id.image);
            }
        }
    }




    ArcToolWindow arcToolWindow;
    MyWindowParamsHelper helper;

    public void createWindow() {

        helper = new MyWindowParamsHelper();

        windowManager = (WindowManager) mActivity.getSystemService(Context.WINDOW_SERVICE);
        arcToolWindow = new ArcToolWindow.Builder(this, windowManager,helper.makeWindowParams(WindowManager.LayoutParams.TYPE_APPLICATION
                |WindowManager.LayoutParams.FLAG_DIM_BEHIND|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE))
                .setAllLevel(1)  //总层数
                .setBgColor(Color.BLACK)  //背景颜色
                .setBgAlpha(180)  //背景透明度 0-255
                .setMaxRadius(helper.LEVEL_ONE_TOP_RADIUS)  //背景高度
                .setCenterBackgroundColor(Color.TRANSPARENT)   // 关闭按钮背景颜色
                .setCenterViewRadius(100)   //关闭按钮高度
                .setCenterViewIconResid(R.mipmap.menu_game_icon,60,new Point(12,12))   //关闭按钮图标，大小，坐标
                .setFadeItems(helper.makeFadeItem(new int[]{10}, new int[ ]{454}))  //分割线参数
                //层参数
                .setLevelItems(helper.makeLevelItem(
                        new int[]{helper.LEVEL_ONE_TOP_RADIUS}
                        , new int[]{4}
                        , new RecyclerView.Adapter[]{new Adapter()}
                        , new CircleLayoutManager[]{new CircleLayoutManager(helper.TOOL_ITEM_RADIUS)}))
                //菜单参数，菜单点击监听
                .setMenuItem(helper.makeMenuItem(mActivity, new MyMenuClick()))
                //关闭菜单点击监听
                .setCenterViewClickListener(new MyCloseClick())
                //外部点击回调
                .setOutsideClickLisitener(new ArcOutsideClickLisitener() {
                    @Override
                    public void onClickOutside() {
                        arcToolWindow.dismss();
                    }
                })
                .create();



//
//        ViewGroup viewGroup = arcToolWindow.getArcRootLyt();
//        setContentView(viewGroup, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }



    public class MyMenuClick implements MyWindowParamsHelper.MenuOnclickLisitener{

        @Override
        public void menuOnClick(int index) {
            switch (index) {
                case 0:
                    Toast.makeText(mActivity, "点击了游戏", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(mActivity, "点击了工具", Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(mActivity, "点击了我的", Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

    public class MyCloseClick implements CenterViewClickListener {

        @Override
        public void onClick() {
            if (arcToolWindow.getLevelVisibility(0) == View.VISIBLE) {
                arcToolWindow.updateBackgroudColor(Color.GREEN);
                arcToolWindow.updateMaxRadius(420);
                arcToolWindow.hideLevelItem(0);
                helper.getLayoutManagers()[0].setRadius(helper.TOOL_ITEM_RADIUS);
            } else {
                arcToolWindow.showLevelItem(0);
                arcToolWindow.updateBackgroudColor(Color.BLACK);
                arcToolWindow.updateMaxRadius(600);
                helper.getLayoutManagers()[0].setRadius(helper.TOOL_ITEM_RADIUS-10);
            }
            /**
             * 更改完参数调用一边此方法套用
             */
            arcToolWindow.update(); //更改完参数调用一边此方法套用
        }
    }
}
